// pages/showCreateVoteBoxSheet/showCreateVoteBoxSheet.js
Page({
  data:{
    voteId:'',
    createTime: '',
    voterId:' ',
    voterList: [],
    pdfURL:'',
    pdfURLFlag: true,
    copyPdfURLBtnDisabled: false,
  },
  onLoad(options){
    const voteId = options.voteId;
    console.log(voteId);
    this.setData({
      voteId: voteId,
    })
  
  },

  onTapScanBtn(){

    wx.scanCode({

      success: (res) => {
        console.log(res);
        try { 
          let voteId = this.data.voteId;

          let jsonForUserInfo = JSON.parse(res.result);
          let createTime = jsonForUserInfo.createTime;
          let voterId = jsonForUserInfo.userId;
          console.log(voteId);
          console.log(createTime);
          console.log(voterId);
  
     
  
          this.setData({
            createTime: createTime,
            voterId: voterId,
          })
          
          let self = this;
          let newVoterList = this.data.voterList;
          wx.request({
         url: "https://www.intohash.com/vote_backend/voteboxuser/addUserToBox/" + voteId,
          //  url: "http://localhost:8001/vote_backend/voteboxuser/addUserToBox/" + voteId,
            method:"POST",
            data:{
              createTime: this.data.createTime,
              userId: this.data.voterId
  
            },   
            success: function(e){
              console.log(e);
              if(e.data.success){
                wx.showToast({  
                  title: "添加成功",   
                  icon: 'none'  
                })
                newVoterList.push(voterId);
                self.setData({
                  voterList: newVoterList,
                  copyPdfURLBtnDisabled: false,
                })
              }
              else{ // 投票用户已存在
                wx.showToast({  
                  title: "该投票用户已存在",   
                  icon: 'none'  
                })
              }
            },
            fail: function(e){
              wx.showToast({  
                title: "添加失败",   
                icon: 'none'  
              })
              console.error(e);
            }
          })
          
        } catch (error) {
          console.log(error);
          wx.showToast({  
            title: "请扫描有效的二维码",   
            icon: 'none'  
          })
        }

      },
      fail: (res) => {
        // wx.showToast({  
        //   title: "添加失败",   
        //   icon: 'none'  
        // })
        // console.log(res);
      }
    })
  },
  
    onTapCopyPdfURLBtn(){
      if(this.data.voterList.length == 0){
        wx.showToast({  
          title: "请先通过扫一扫添加投票人",   
          icon: 'none'  
        })
      }
      else{
        let voteId = this.data.voteId;
        let self = this;
      wx.request({
        
      url: "https://www.intohash.com/vote_backend/main/creatPdf/" + voteId,
      // url: "http://localhost:8001/vote_backend/main/creatPdf/" + voteId,
        method:"get",
  
        success: function(e){
         
          console.log(e);
          let pdfURL = null;
          console.log(e.data.success);
          if(e.data.success){
            console.log("成功获得投票用纸的URL");
            pdfURL = e.data.data;
            self.setData({
              pdfURL: pdfURL,
            })
            wx.setClipboardData({ // 将pdfURL复制到粘贴板
              data: self.data.pdfURL,
              success: function(res){
                wx.showToast({
                  title: '复制成功',
                })
              },
              fail: function(res){
                wx.showToast({
                  title: '复制失败',
                })
              }
            })
          }
          else {
            console.log("未成功获得投票用纸的URL");
            wx.showToast({  
              title: "未成功获得投票用纸的URL",   
              icon: 'none'  
            })
          }
          self.setData({
            copyPdfURLBtnDisabled: true,
          })
        },
        fail: function(e){
          console.log("未成功获得投票用纸的URL");
          wx.showToast({  
            title: "未成功获得投票用纸的URL",   
            icon: 'none'  
          })
          console.error(e);
        }
      })
      }
      
  },

  onTapStartVote(){

    if(this.data.voterList.length == 0){
      wx.showToast({  
        title: "请先通过扫一扫添加投票人",   
        icon: 'none'  
      })
    }
    else{
      wx.redirectTo({       
       // url: '/pages/startVoteBoxVote/startVoteBoxVote?voteId=' + this.data.voteId + '&type=1'
       url: '/pages/startVoteBoxVote/startVoteBoxVote?voteId=' + this.data.voteId
      }) 
    }



  }

})
